import {doGet} from "@/config/http";

const isFullScreen = false

export function setDes(list, arr1, arr2, svc, url, field, desKey, resAction) {
    if (list?.length > 0 && arr1?.length > 0) {
        let set = new Set();
        list.map(item => {
            arr1.map(key => item[key] && set.add(item[key]))
        })
        if (set?.size > 0) {
            let params = {
                field: field,
                values: Array.from(set).join(',')
            }
            doGet(svc, url, params, isFullScreen).then(res => {
                if (res?.length > 0) {
                    if (resAction) {
                        resAction(res)
                    } else {
                        let resMap = new Map()
                        res.map(u => {
                            resMap.set(String(u[field]), u)
                        })
                        list.map((item) => {
                            arr1.map((key, i) => {
                                item[arr2[i]] = resMap.has(item[key]) && resMap.get(item[key])[desKey] || ''
                            })
                        })
                    }
                }
            })

        }
    }
    if (Object.prototype.toString.call(list) === '[object Object]') {
        let set = new Set();
        arr1.map(key => list[key] && set.add(list[key]))
        if (set?.size > 0) {
            let params = {
                field: field,
                values: Array.from(set).join(',')
            }
            doGet(svc, url, params, isFullScreen).then(res => {
                if (res?.length > 0) {
                    let resMap = new Map()
                    res.map(u => {
                        resMap.set(String(u[field]), u)
                    })
                    arr1.map((key, i) => {
                        list[arr2[i]] = resMap.has(list[key]) && resMap.get(list[key])[desKey] || ''
                    })
                }
            })
        }
    }
}

export function setDes1(list, arr1, arr2, svc, url, field, desKey) {
    return new Promise((resolve, reject) => {
        if (list?.length === 0 || arr1?.length === 0) {
            resolve()
        }
        let set = new Set();
        list.map(item => {
            arr1.map(key => item[key] && set.add(item[key]))
        })
        if (set?.size === 0) {
            resolve()
        }
        let params = {
            field: field,
            values: Array.from(set).join(',')
        }
        doGet(svc, url, params, isFullScreen).then(res => {
            if (res?.length > 0) {
                let resMap = new Map()
                res?.map(u => {
                    resMap.set(String(u[field]), u)
                })
                list.map((item) => {
                    arr1.map((key, i) => {
                        item[arr2[i]] = resMap.has(item[key]) && resMap.get(item[key])[desKey] || ''
                    })
                })
            }
            resolve()
        })
    })
}

export function echoFactory(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/factory/listByField", 'werksD', 'name1');
}

// arr3为对应的工厂代码取值
export function echoLocation(list, arr1, arr2, arr3) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/location/listByField", 'lgortD', 'zlgobe', res => {
        let resMap = new Map()
        res.map(u => {
            resMap.set(String(`${u['lgortD']}-${u['werksD']}`), u)
        })
        list.map((item) => {
            arr1.map((key, i) => {
                const itemkey = `${item[key]}-${item[arr3[i]]}`
                item[arr2[i]] = resMap.has(itemkey) && resMap.get(itemkey)['zlgobe'] || ''
            })
        })
    });
}

export function echoMaterial(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/material/listByField", 'code', 'name');
}

export async function echoMaterialdes(list, arr1, arr2, resAction) {
    await setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/material/listByField", 'code', 'description', resAction);
}

export function echoSupplier(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/supplier/listByField", 'lifnr', 'name1Gp');
}

export function echoCustomer(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/tb-customer/listByField", 'code', 'fullName');
}

export function echomaterialgroup(list, arr1, arr2, resAction) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/material-group/listByField", 'materialGroupCode', 'materialGroupDesc', resAction);
}

export async function echomaterialgroupasync(list, arr1, arr2) {
    await setDes1(list, arr1, arr2, 'peas-module-sys-mdm', "/material-group/listByField", 'materialGroupCode', 'materialGroupDesc');
}

export function echobrand(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/material-group/listByField", 'brandCode', 'brandDesc');
}

export function echosaleorg(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/sales-org/listByField", 'salesOrgCode', 'salesOrgDescription');
}

export async function echosaleorgasync(list, arr1, arr2) {
    await setDes1(list, arr1, arr2, 'peas-module-sys-mdm', "/sales-org/listByField", 'salesOrgCode', 'salesOrgDescription');
}

export function echobusinessorg(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/dealin-org/listByField", 'dealinOrgCode', 'dealinOrgName');
}

//组织机构
//计划中心的申请单位
export function echdepart(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-auth-maintain', "/depart/listByField", 'orgCode', 'orgName');
}

// 采购组
export function echopurchasegroup(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/purchase-group/listByField", 'ekgrp', 'eknam');
}

export async function echopurchasegroupasync(list, arr1, arr2) {
    setDes1(list, arr1, arr2, 'peas-module-sys-mdm', "/purchase-group/listByField", 'ekgrp', 'eknam');
}

//省市区
export function echregion(list, arr1, arr2) {
    setDes(list, arr1, arr2, 'peas-module-sys-mdm', "/region/listByField", 'regionCode', 'regionDesc')
}

export async function echregionasync(list, arr1, arr2) {
    await setDes1(list, arr1, arr2, 'peas-module-sys-mdm', "/region/listByField", 'regionCode', 'regionDesc')
}

//盘点任务主表
export function echoChecking(list, arr1, back) {
    if (list?.length > 0 && arr1?.length > 0) {
        let set = new Set();
        list.map(item => {
            arr1.map(key => item[key] && set.add(item[key]))
        })
        if (set?.size > 0) {
            doGet('peas-module-stock-biz', "/checking/ids", {ids: Array.from(set).join(',')}, false).then(res => {
                if (res?.length > 0) {
                    list.map(item => {
                        let mainObj = res.find(main => main.id === item.checkingId)
                        if (mainObj) {
                            delete mainObj.id
                            Object.assign(item, mainObj)
                        }
                    })
                    back && back()
                }
            })
        }
    }
}
